package com.hmall.order.web;

import com.hmall.order.constant.MqConstant;
import com.hmall.order.dto.OrderDTO;
import com.hmall.order.pojo.Order;
import com.hmall.order.service.IOrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
public class OrderController {
   @Autowired
   private RabbitTemplate rabbitTemplate;

   @Autowired
   private IOrderService orderService;

   @GetMapping("{id}")
   public Order queryOrderById(@PathVariable("id") Long orderId) {
      return orderService.getById(orderId);
   }
   @PostMapping
   public Long generateOrders(@RequestBody OrderDTO orderDTO) {
      Long id = orderService.generateOrders(orderDTO);
      MessageProperties messageProperties = new MessageProperties();
      messageProperties.setHeader("x-delay",18000);
      Message message = new Message(String.valueOf(id).getBytes(),messageProperties);
      rabbitTemplate.convertAndSend(MqConstant.ORDER_EXCHANGE,MqConstant.DELAY_KEY,message);
      return id;
   }
}
